﻿using SixLabors.ImageSharp;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;

namespace ConsoleApp
{
    internal class Program
    {
        class NewExpressionVisitor : ExpressionVisitor
        {
            public ParameterExpression _NewParameter { get; private set; }

            public NewExpressionVisitor(ParameterExpression param)
            {
                _NewParameter = param;
            }

            public Expression Replace(Expression exp)
            {
                return Visit(exp);
            }

            protected override Expression VisitParameter(ParameterExpression node)
            {
                return _NewParameter;
            }
        }
        class Student
        {
            public string Name { get; set; }

            public Student(string name, int age)
            {
                Name = name;
                Age = age;
            }

            public int Age { get; set; }
        }
        static void Main(string[] args)
        {
            List<Student> students = new List<Student>
            {
                new Student("A",18),
                new Student("B",19),
                new Student("A",19),
            };

            Expression<Func<Student, bool>> whereFunc = (student) => student.Age == 19;

            ParameterExpression parameterExpression = Expression.Parameter(typeof(Student), "c");
            NewExpressionVisitor newExpressionVisitor = new NewExpressionVisitor(parameterExpression);
            Expression left = newExpressionVisitor.Replace(whereFunc.Body);
            Expression<Func<Student, bool>> tempFunc = (student) => student.Name == "A";

            Expression right = newExpressionVisitor.Replace(tempFunc.Body);
            BinaryExpression body = Expression.And(left, right);
            whereFunc =Expression.Lambda<Func<Student, bool>>(body, new ParameterExpression[1] { parameterExpression });

            var r =students.Where(whereFunc.Compile()).ToList();
            Console.WriteLine(r.Count);
        }
        /// <summary>
        /// 根据原图裁剪生成正方形图片
        /// </summary>
        /// <param name="sourceImagePath"></param>
        /// <param name="targetImagePath"></param>
        /// <param name="pixel">像素</param>
        public static void GenerateSquare(string sourceImagePath, string targetImagePath, int pixel)
        {
            using (var inputStream = System.IO.File.OpenRead(sourceImagePath))
            using (var image = Image.Load<Rgba32>(inputStream))
            {
                if (image.Width != image.Height)
                {
                    int left = default;
                    int top = default;
                    int right = default;
                    int bottem = default;

                    // 高与宽相差
                    int differ = default;
                    // 横图
                    if (image.Width > image.Height)
                    {
                        top = 0;
                        bottem = image.Height;
                        // 相差=宽-高
                        differ = image.Width - image.Height;
                        left = differ / 2;
                        right = differ / 2 + image.Height;
                    }
                    // 竖图
                    else
                    {
                        left = 0;
                        right = image.Width;
                        // 相差=宽-高
                        differ = image.Height - image.Width;
                        top = differ / 2;
                        bottem = differ / 2 + image.Width;
                    }

                    image.Mutate(x => x.Crop(Rectangle.FromLTRB(left, top, right, bottem)));
                }

                if (image.Width != pixel)
                {
                    //var multiple = image.Width / pixel;
                    //var len = (int)(image.Width / multiple);
                    image.Mutate(x => x.Resize(pixel, pixel));
                }
                image.Save(targetImagePath);
            }
        }
    }
}
